/**
 * 敏感词配置文件
 * 用于存储需要过滤的敏感词列表
 */

// 使用 Vite 的 ?raw 功能导入原始文本内容
import systemSensitiveWordsRaw from '../core/filters/system_sensitive_words.txt?raw'
import filterConfigRaw from './sensitive_filter_config.json?raw'

// 解析敏感词过滤配置
let filterConfig = {
  enableSensitiveWordFilter: true,
  allowUserConfig: true
}

try {
  if (filterConfigRaw && typeof filterConfigRaw === 'string') {
    const parsedConfig = JSON.parse(filterConfigRaw)
    filterConfig = {
      enableSensitiveWordFilter:
        parsedConfig.enableSensitiveWordFilter !== undefined ? parsedConfig.enableSensitiveWordFilter : true,
      allowUserConfig: parsedConfig.allowUserConfig !== undefined ? parsedConfig.allowUserConfig : true
    }
    console.log('[SensitiveWords] Loaded filter config:', filterConfig)
  } else {
    console.warn(
      '[SensitiveWords] sensitive_filter_config.json content is not a string or is empty. Using default config.'
    )
  }
} catch (error) {
  console.error('[SensitiveWords] Error processing filter config from raw import:', error)
}

// 解析系统内置敏感词列表 (不可修改)
let systemSensitiveWords: string[] = []

// 只有当配置允许敏感词过滤时，才加载系统敏感词
if (filterConfig.enableSensitiveWordFilter) {
  try {
    if (systemSensitiveWordsRaw && typeof systemSensitiveWordsRaw === 'string') {
      systemSensitiveWords = systemSensitiveWordsRaw
        .split(/\r?\n/) // 支持 Windows (CRLF) 和 Unix (LF) 换行符
        .map((word) => word.trim())
        .filter((word) => word.length > 0)
      if (systemSensitiveWords.length === 0) {
        console.warn(
          '[SensitiveWords] Loaded system sensitive words, but the list is empty. Check system_sensitive_words.txt content.'
        )
      }
    } else {
      console.warn(
        '[SensitiveWords] system_sensitive_words.txt raw content is not a string or is empty. Path: ../core/filters/system_sensitive_words.txt'
      )
    }
  } catch (error) {
    console.error('[SensitiveWords] Error processing system sensitive words from raw import:', error)
  }
}

// 用户自定义敏感词列表 (用于初始化，后续会从用户设置加载)
export const userCustomSensitiveWords: string[] = [
  '政治话题',
  '暴力言论',
  '色情内容',
  '赌博',
  '毒品',
  '脏话'
  // 可根据需要添加更多敏感词
]

// 敏感词过滤配置
export const sensitiveWordsConfig = {
  // 是否启用敏感词过滤（根据配置文件决定初始值）
  enabled: filterConfig.enableSensitiveWordFilter,
  // 是否允许用户配置敏感词过滤功能
  allowUserConfig: filterConfig.allowUserConfig,
  // 用户自定义的敏感词列表
  words: filterConfig.enableSensitiveWordFilter ? userCustomSensitiveWords : [],
  // 系统内置的敏感词列表 (只读)
  get systemWords() {
    return [...systemSensitiveWords]
  }
}
